Imports System.Collections.Generic
Imports System.Data
Imports System.Data.Entity
Imports System.Linq

Public Class <OwnerU>_<TableU>DataClass

    Public Function SelectAll() As DataTable
        Using context = New <Database>DBContext()
<If Loop>
      <IsFKeyCount>
            Dim Query = (From _<OwnerU>_<TableU> In context.<OwnerU>_<TableU>
     	<ExitLoop>
</If Loop>
<If Loop>
     <IsFKeyCount>
     <And>
     <IsRelated>
     <And>
     <IsMiscTag>
                        join A<MiscTag>_<ROwner>_<RTable> in context.<ROwner>_<RTable> on _<OwnerU>_<TableU>.<Field> equals A<MiscTag>_<ROwner>_<RTable>.<RField>
     <IsFKeyCount>
     <And>
     <IsRelated>
     <And>
     <IsNotMiscTag>
                        join _<ROwner>_<RTable> in context.<ROwner>_<RTable> on _<OwnerU>_<TableU>.<Field> equals _<ROwner>_<RTable>.<RField>
</If Loop>
<If Loop>
      <IsFKeyCount>
                        Select New <OwnerU>_<TableU>Joined() With { _
     	<ExitLoop>
</If Loop>
<If Loop>
     <IsFKeyCount>
     <And>
     <IsRelated>
     <And>
     <IsMiscTag>
     <And>
     <IsCurRow=[0]>
                            .<Field> = A<MiscTag>_<ROwner>_<RTable>.<RDisplayFieldU> _
     <IsFKeyCount>
     <And>
     <IsRelated>
     <And>
     <IsMiscTag>
     <And>
     <IsCurRow<>[0]>
                           ,.<Field> = A<MiscTag>_<ROwner>_<RTable>.<RDisplayFieldU> _
     <IsFKeyCount>
     <And>
     <IsRelated>
     <And>
     <IsNotMiscTag>
     <And>
     <IsCurRow=[0]>
                            .<Field> = _<ROwner>_<RTable>.<RDisplayFieldU> _
     <IsFKeyCount>
     <And>
     <IsRelated>
     <And>
     <IsNotMiscTag>
     <And>
     <IsCurRow<>[0]>
                           ,.<Field> = _<ROwner>_<RTable>.<RDisplayFieldU> _
     <IsFKeyCount>
     <And>
     <IsNotRelated>
     <And>
     <IsCurRow=[0]>
                            .<Field> = _<OwnerU>_<TableU>.<Field> _
     <IsFKeyCount>
     <And>
     <IsNotRelated>
     <And>
     <IsCurRow<>[0]>
                           ,.<Field> = _<OwnerU>_<TableU>.<Field> _
</If Loop>
<If Loop>
      <IsFKeyCount>
                        }).AsQueryable()

            Return Query.ToList().ToDataTable()
     	<ExitLoop>
</If Loop>
<If Loop>
      <IsNotFKeyCount>
            Return context.<OwnerU>_<TableU>.ToList().ToDataTable()
     	<ExitLoop>
</If Loop>
        End Using
    End Function

    Public Function Select_Record(_<OwnerU>_<TableU> As <OwnerU>_<TableU>) As <OwnerU>_<TableU>
        Using context = New <Database>DBContext()
            Return context.<OwnerU>_<TableU>.Find(
    <If Loop>
      <IsPKey>
      <And>
      <IsCurRow=[0]>
                                        _<OwnerU>_<TableU>.<FieldU> _
      <IsPKey>
      <And>
      <IsCurRow<>[0]>
                                        , _<OwnerU>_<TableU>.<FieldU> _
    </If Loop>
                                        )
        End Using
    End Function

    Public Function Add(_<OwnerU>_<TableU> As <OwnerU>_<TableU>) As Boolean
        Using context = New <Database>DBContext()
            context.<OwnerU>_<TableU>.Add(_<OwnerU>_<TableU>)
            Return context.SaveChanges() > 0
        End Using
    End Function

    Public Function Update(_<OwnerU>_<TableU> As <OwnerU>_<TableU>) As Boolean
        Using context = New <Database>DBContext()
            context.<OwnerU>_<TableU>.Attach(_<OwnerU>_<TableU>)
            context.Entry(_<OwnerU>_<TableU>).State = System.Data.Entity.EntityState.Modified
            Return context.SaveChanges() > 0
        End Using
    End Function

    Public Function Delete(_<OwnerU>_<TableU> As <OwnerU>_<TableU>) As Boolean
        Using context = New <Database>DBContext()
            context.<OwnerU>_<TableU>.Remove(context.<OwnerU>_<TableU>.Find(
    <If Loop>
      <IsPKey>
      <And>
      <IsCurRow=[0]>
                                        _<OwnerU>_<TableU>.<FieldU>
      <IsPKey>
      <And>
      <IsCurRow<>[0]>
                                        ,_<OwnerU>_<TableU>.<FieldU>
    </If Loop>
                                        ))
            Return context.SaveChanges() > 0
        End Using
    End Function

End Class
